/**
 * 152. 乘积最大子数组
 * https://leetcode-cn.com/problems/maximum-product-subarray/
 */

/**
 * @param {number[]} nums
 * @return {number}
 */
function maxProduct(nums) {
  let [max] = nums;
  let preMax = max;
  let preMin = max;
  for (let i = 1; i < nums.length; i += 1) {
    const x = preMax * nums[i];
    const y = preMin * nums[i];
    preMax = Math.max(x, y, nums[i]);
    preMin = Math.min(x, y, nums[i]);
    max = Math.max(preMax, max);
  }
  return max;
}

console.log(maxProduct([2, 3, -2, 4]) === 6);
console.log(maxProduct([-2, 0, -1]) === 0);
console.log(maxProduct([-4, -3, -2]) === 12);
